<?php

namespace App\Http\Controllers\Course;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\CourseExperienceCard as ExperienceCard;
use App\Models\ExperienceCardRecord as CardRecord;
use App\Models\Ctype;
use Illuminate\Support\Facades\DB;
use App\User;
class CourseExperienceCardController extends Controller
{
    public function __construct()
    {

    }

    public function index(Request $request){
        $mobile = $request->input("mobile",0);
        $msg = 0;
        if($mobile){
            $user = DB::table("users")->where("mobile", $mobile)->select("id")->first();
            if($user){
                $item = DB::table("expericence_card_records")->where("user_id",$user->id)->where("type_id",0)->first();
                if($item){
                    $msg = "记录已存在";
                }else{
                    $cur_arr = array();
                    $cur_arr['user_id'] = $user->id;
                    $cur_arr['cards_id']= 0;
                    $cur_arr['type_id'] = 0;
                    $cur_arr['start_time'] = date("Y-m-d H:i:s");
                    $cur_arr['experience_time'] = 90;
                    $cur_arr['updated_at'] = date("Y-m-d H:i:s");
                    $cur_arr['created_at'] = date("Y-m-d H:i:s");
                    $r = DB::table("expericence_card_records")->insert($cur_arr);
                    if($r){
                        $msg = "添加成功";
                    }else{
                        $msg = "添加失败请联系管理员";
                    }
                }
            }else{
                $msg = "用户不存在或手机号有问题";
            }
        }


        $experienceCard = ExperienceCard::paginate(10);
        return view('courseCard.index',['cards'=>$experienceCard, "msg"=>$msg]);
    }

    public function create(Request $request){
        $types = Ctype::paginate(2);
        return view('courseCard.create',['types'=>$types]);
    }

    public function postCreate(Request $request){
        $start = $request->input('start');
        $end = $request->input('end');
        $type = $request->input('type');
        $name = $request->input('name');
        $card = new ExperienceCard();
        $card->name = $name;
        $card->type_id = $type;
        $card->validate_time = 3;
        $card->status = 1;
        $card->start = $start;
        $card->end = $end;
        if($card->save()){
            $this->getSessionInfo($request,'success','添加成功');
            return $this->getMessage(0,'添加成功');
        }else{
            return $this->getMessage(1,'添加失败');
        }
    }

    public function edit(Request $request,$id){
        $card = ExperienceCard::where('id',$id)->first();
        $types = Ctype::paginate(2);
        return view('courseCard.edit',['card'=>$card,'types'=>$types]);
    }

    public function postEdit(Request $request){
        $start = $request->input('start');
        $end = $request->input('end');
        $type = $request->input('type');
        $name = $request->input('name');
        $id = $request->input('id');
        $card = ExperienceCard::where('id',$id)->first();
        $card->name = $name;
        $card->type_id = $type;
//        $card->validate_time = 3;
//        $card->status = 1;
        $card->start = $start;
        $card->end = $end;
        if($card->save()){
            $this->getSessionInfo($request,'success','添加成功');
            return $this->getMessage(0,'添加成功');
        }else{
            return $this->getMessage(1,'添加失败');
        }
    }

    public function updateState(Request $request){
        $state = $request->input('state');
        $id = $request->input('id');
        $card = ExperienceCard::where('id',$id)->first();
        $card->status = $state;
        if($card->save()){
            return $this->getMessage(0,'更改成功');
        }else{
            return $this->getMessage(1,'更改成功');
        }

    }

    /*
     *用户体验记录
     */
    public function cardRecords(Request $request,$id){
        $name = $request->input('name','');
        $cardRecords = new CardRecord();
        if($name){
            $users = User::where('name','like','%'.$name.'%')->select('id')->get()->toArray();
            $result = [];
            array_map(function ($value) use (&$result) {
                $result = array_merge($result, array_values($value));
            }, $users);
            $cardRecords = $cardRecords->whereIn('user_id',$result);
        }
        $cardRecords = $cardRecords->where('cards_id',$id)->paginate(20);
        return view('courseCard.cardRecords',['cardRecords'=>$cardRecords]);
    }

}